/********************************************************/
/*	Author:		gong_libin			*/
/*	Date:		2017_11_01			*/
/*	File:		CmEvent.h			*/
/********************************************************/

#ifndef	_CMEVENT_H
#define	_CMEVENT_H

#include <string>
#include "CmMap.h"
#include "CmTcp.h"
#include "CmAdxDef.h"

#include <event2/http.h>
#include <event2/event.h>
#include <event2/thread.h>
#include <event2/buffer.h>
#include <event2/http_struct.h>
#include <event2/event_compat.h>
#include <event2/buffer_compat.h>
#include <event2/keyvalq_struct.h>

#define	CM_EVT_TMOUT					3

#define CM_EVT_ADDBUFFER(buffer, data, size)		do {	\
	evbuffer_add(buffer, data, size);			\
} while (0)

#define CM_EVT_ADDPRINTF(buffer, format, args...)	do {	\
	evbuffer_add_printf(buffer, format, ##args);		\
} while (0)

class CCmEvent
{
public:
	CCmEvent() {m_pstBase = event_init();}
	virtual ~CCmEvent() {event_base_free(m_pstBase);}

	void* CmEventDispatch();
	CMADXPARA_S* CmEventParaMalloc();
	void CmEventKvsDel(CMMAP_C* pCKvs);
	void CmEventParaFree(CMADXPARA_S* pstPara);
	CMMAP_C* CmEventKvsGet(struct evhttp_uri* pstUri);
	CMADXPARA_S* CmEventContent(struct evbuffer* pstBuf);
	void CmEventCall(void(*pFun)(struct evhttp_request*, void*), void*);
	void CmEventAddHeader(struct evkeyvalq* pstInput, char* pszKey, char* pszVal);
	const char* CmEventFindHeader(struct evkeyvalq* pstInput, const char* pszKey);
	void CmEventSendReply(struct evhttp_request* pstReq, UCHAR* puszData, int iSize);

	static CmNet::CCmTcp m_CTcp;

	static void CmEventPthread();
	static int CmEventInit(char* pszPort);

protected:
	struct event_base* m_pstBase;

private:

};

#endif /* _CMEVENT_H */
